<?php

namespace Modules\Inventory\Repository;

use Modules\Inventory\Models\Inventory;
use Modules\Inventory\Models\InventoryTrans;

/**
 * 库存变动记录
 * Class InventoryRepository
 * @package Modules\Production\Repository
 */
class InventoryTransRepository
{

    /**
     * 保存一条库存变动记录
     * @param $data
     * @return mixed
     */
    static public function save($data){
        return (new InventoryTrans)->storeBy($data);
    }

    /**
     * 根据箱号查询库存记录
     * @param $lotnum
     */
    static public function getTransByLotnum($lotnum){
        return InventoryTrans::orWhere('from_lotnum',$lotnum)->orWhere('to_lotnum',$lotnum)->orderBy('created_at','desc')->get();
    }

    /**
     * 按单据id删除库存变动记录
     * @param $docid
     * @return mixed
     */
    static public function deleteByDocId($docid){
        return InventoryTrans::where('doc_id',$docid)->delete();
    }

    /**
     * 根据条件查询库存变动记录
     * @param $orgCode
     * @param $cargoCode
     * @param $materialNo
     * @param $lotnum
     * @param $location
     * @return mixed
     */
    static public function getBy($orgCode,$cargoCode,$materialNo,$lotnum,$location){
        return InventoryTrans::where('org_code',$orgCode)
                ->where('cargo_code',$cargoCode)
                ->where('material_no',$materialNo)
                ->where('to_lotnum',$lotnum)
                ->where('to_location',$location)
                ->orderBy('created_at','asc')
                ->orderBy('id','asc')
                ->get();
    }

    /**
     * 根据id修改
     * @param $id
     * @param $data
     * @return mixed
     */
    static public function updateBy($id,$data){
        return InventoryTrans::where('id',$id)->update($data);
    }
}
